package com.cqrt.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqrt.constant.RTErrorCodeEnum;
import com.cqrt.service.ProjectChangeService;
import com.cqrt.service.RTProjectService;
import com.hustcad.plm.pdm.core.response.TyppmResponseResult;
import com.hustcad.plm.rpm.model.vo.change.CreateChangeInfoRequestVO;
import com.hustcad.plm.rpm.model.vo.change.PlanChangeInfoVO;
import com.hustcad.plm.rpm.service.change.TyppmChangeInfoService;
import com.ty.ppm.model.constant.ApiPathConstant;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(ApiPathConstant.PARENT_VERSION_ONE_PATH + "/change")
public class ProjectChangeController {

    @Resource
    private ProjectChangeService projectChangeService;
    @Resource
    private RTProjectService projectService;

    /*
     * 创建计划变更单
     */
    @PostMapping("/createPlanChangeForm")
    public TyppmResponseResult createPlanChangeForm(@RequestBody JSONObject requestVO) {
        CreateChangeInfoRequestVO changeInfo = projectService.createPlanChangeForm(requestVO);
        return TyppmResponseResult.success(changeInfo) ;
    }

    /*
     * 暂停或启动计划
     */
    @PostMapping("/pauseOrRestartPlan")
    public TyppmResponseResult pauseOrRestartPlan(@RequestBody JSONObject changeInfo) {
        try {
            projectChangeService.pauseOrRestartPlan(changeInfo);
        } catch (Exception e) {
            e.printStackTrace();
            throw RTErrorCodeEnum.PROJECT_PARAMETER_NOT_EXIST.getException(e.getMessage());
        }
        return TyppmResponseResult.success();
    }

    @PostMapping("/projectStopOrReStart")
    public TyppmResponseResult projectStopOrReStart(@RequestBody JSONObject params) {
        String oaFormDataUrl = null;
        try {
            oaFormDataUrl = projectChangeService.projectStopOrReStart(params);
        } catch (Exception e) {
            e.printStackTrace();
            throw RTErrorCodeEnum.PROJECT_PARAMETER_NOT_EXIST.getException(e.getMessage());
        }
        return TyppmResponseResult.success(oaFormDataUrl);
    }

    /*
     * 暂停或启动计划
     */
    @PostMapping("/approvedChangeWorkFlow")
    public JSONObject approvedChangeWorkFlow(@RequestBody JSONObject changeInfo) {
        JSONObject result = new JSONObject();
        try {
            changeInfo.put("route", "通过");
            projectChangeService.pauseOrRestartPlan(changeInfo);
            result.put("success","true");
            result.put("message","成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", "false");
            result.put("message", e.getMessage());
        }
        return result;
    }
}
